/*小区的翻译 : block 是否更加合适?*/

define(function () {
    var template=`
        <div class="c-two-left">
        
            <div class="c-center-box"> 
                <div class="c-two-center"> 
                    <i-table :columns="columnsHead" @on-row-click="blockrowclicked" :data="plotTableData" size="small" ref="table"></i-table> 
                </div>
            
            </div>
        
        </div>
    `

    var BlockTable={
        name:"block-table",
        template:template,
        data: function () {
            return {
                columnsHead: [
                    {
                        title: '锁定',
                        width: 40,
                        key: 'lock',
                        render:(function (){
                            return (
                                '<p class="noLockImg"></p>')
                        })
                    },
                    {
                        title: '小区名称',
                        key: 'Name'
                    },
                    {
                        title: '拼音',
                        key: 'Pinyin'
                    },
                    {
                        title: '片区',
                        key: 'CustomAreaName'
                    },
                    {
                        title: '详细地址',
                        key: 'AddressDetail'
                    },
                    /*
                    {
                        title: '楼栋数',
                        key: 'BuildingAmount'
                    },
                    {
                        title: '房号规则',
                        key: 'RoomNo'
                    },*/
                    {
                        title: '用途',

                        render:function( h,params){
                             return h('span',params.row.BlockModel.PropertyUsageName)

                        }
                    },
                    {
                        title: '类型',
                        render:function( h,params){
                            return h('span',params.row.PropertyTypeName)

                        }
                    },
                    {
                        title: '建房',
                        render:function( h,params){
                            return h('span',params.row.BlockModel.BuiltTime)

                        }
                    },
                    /*
                    {
                        title: '选栋',
                        width: 50,
                        key: 'BuindingMustSelect',

                        render:(function (params){
                            return (
                            '<Checkbox v-model="['+params._index+'].checked"></Checkbox>')
                        })

                    },
                    {
                        title: '选房',
                        width: 50,
                        key: 'RoomMustSelect',
                        render:(function (params){
                            return (
                            '<Checkbox v-model="plotTableData['+params._index+'].checked"></Checkbox>')
                        })
                    },*/

                ],
                plotTableData: [

                ]
            }
        },
        methods:{
            blockrowclicked:function(data){

                console.log(data)
                   window.bus.$emit("editblock",data);//通过 bus 发布事件.

            }
        },
        created:function( )
        {
            var that=this;
            //通过bus,监听事件
            window.bus.$on("blocksearch",function(data){
                if(data.IsSuccess)
                {
                that.plotTableData=data.ResultObject;
                }
                else {
                    that.$Message.error(data.ErrMsg);
                }
            })
        },
        mounted: function ()
        {
            var that = this

            window.bus.$on("block_save_success", function (data) {
                var thatData = that.plotTableData
                var i = thatData.length
                var isExisted = false
                while (i--) {
                    if (thatData[i].Id==data.Id) {
                        thatData[i] = data
                        isExisted = true
                        break
                    }
                }
                if (!isExisted) {
                    that.plotTableData.push(data)
                }
                else {
                    that.plotTableData = thatData.slice()
                }

            })
        }

    }
    return BlockTable
})
